/*
 *  Copyright 2013 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * AbstractEntityPanel.java
 *
 * Created on 13.01.2013, 18:34:29
 */

package org.opu.db.obj.vis.ui.client.widgets;

import java.io.Serializable;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.opu.db.obj.vis.domain.domains.AbstractEntity;
import org.opu.db.obj.vis.domain.domains.AbstractPriority;
import org.opu.db.obj.vis.ui.client.i18n.AbstractI18N;

/**
 *
 * @author yura
 */
public class AbstractEntityPanel<Id extends Serializable, 
        Priority extends AbstractPriority<Entity>, 
        Entity extends AbstractEntity<Priority, Id>>
        extends JPanel {
    
    public static final String I18N = "org.opu.db.obj.vis.ui.resource.PanelIs18n";
    protected AbstractI18N panelIs18n = new AbstractI18N(I18N);
    protected DefaultTableModel notionTableModel = new DefaultTableModel(0, 1);

    protected Entity entity;

    public AbstractEntityPanel() {
    }

    public void init(Entity entity) {
        this.entity = entity;
        initComponents();
    }

    /** Creates new form AbstractEntityPanel */
    public AbstractEntityPanel(Id id, Priority priority, Entity entity) {
        this.entity = entity;
        initComponents();
    }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        idPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        descriptionPanel = new javax.swing.JPanel();
        descriptionScroll = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        prioritiesPanel = new javax.swing.JPanel();
        notionPanel = new javax.swing.JPanel();
        notionScrollPanel = new javax.swing.JScrollPane();
        notionTable = new javax.swing.JTable();
        notionDeleteBtn = new javax.swing.JButton();
        notionRefreshBtn = new javax.swing.JButton();
        priorityValuePanel = new javax.swing.JPanel();
        priorityValueSpiner = new javax.swing.JSpinner();

        setLayout(new java.awt.BorderLayout());

        idPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(panelIs18n.getString("id.title"))); // NOI18N
        idPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setText(entity.getId().toString());
        idPanel.add(jLabel1, java.awt.BorderLayout.CENTER);

        add(idPanel, java.awt.BorderLayout.PAGE_START);

        descriptionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(panelIs18n.getString("description.title"))); // NOI18N
        descriptionPanel.setLayout(new java.awt.BorderLayout());

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setRows(3);
        descriptionScroll.setViewportView(descriptionTextArea);

        descriptionPanel.add(descriptionScroll, java.awt.BorderLayout.PAGE_START);

        notionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(panelIs18n.getString("notion.title"))); // NOI18N

        notionTable.setModel(notionTableModel);
        notionTable.setTableHeader(null);
        notionScrollPanel.setViewportView(notionTable);

        notionDeleteBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/opu/db/obj/vis/ui/resource/icons/small/Delete.png"))); // NOI18N
        notionDeleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                notionDeleteBtnActionPerformed(evt);
            }
        });

        notionRefreshBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/opu/db/obj/vis/ui/resource/icons/small/Refresh.png"))); // NOI18N
        notionRefreshBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                notionRefreshBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout notionPanelLayout = new javax.swing.GroupLayout(notionPanel);
        notionPanel.setLayout(notionPanelLayout);
        notionPanelLayout.setHorizontalGroup(
            notionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(notionPanelLayout.createSequentialGroup()
                .addComponent(notionRefreshBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE)
                .addComponent(notionDeleteBtn)
                .addContainerGap())
            .addComponent(notionScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
        );
        notionPanelLayout.setVerticalGroup(
            notionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, notionPanelLayout.createSequentialGroup()
                .addComponent(notionScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(notionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(notionRefreshBtn)
                    .addComponent(notionDeleteBtn)))
        );

        priorityValuePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(panelIs18n.getString("priority.title"))); // NOI18N

        javax.swing.GroupLayout priorityValuePanelLayout = new javax.swing.GroupLayout(priorityValuePanel);
        priorityValuePanel.setLayout(priorityValuePanelLayout);
        priorityValuePanelLayout.setHorizontalGroup(
            priorityValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(priorityValuePanelLayout.createSequentialGroup()
                .addComponent(priorityValueSpiner, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                .addContainerGap())
        );
        priorityValuePanelLayout.setVerticalGroup(
            priorityValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(priorityValuePanelLayout.createSequentialGroup()
                .addComponent(priorityValueSpiner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout prioritiesPanelLayout = new javax.swing.GroupLayout(prioritiesPanel);
        prioritiesPanel.setLayout(prioritiesPanelLayout);
        prioritiesPanelLayout.setHorizontalGroup(
            prioritiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(prioritiesPanelLayout.createSequentialGroup()
                .addComponent(notionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(priorityValuePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );
        prioritiesPanelLayout.setVerticalGroup(
            prioritiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(prioritiesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(prioritiesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(priorityValuePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(notionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        descriptionPanel.add(prioritiesPanel, java.awt.BorderLayout.CENTER);

        add(descriptionPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void notionDeleteBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_notionDeleteBtnActionPerformed
        int selectedRow = notionTable.getSelectedRow();
        
        if (selectedRow > 0) {
            notionTableModel.removeRow(selectedRow);
        }

        notionTable.revalidate();
    }//GEN-LAST:event_notionDeleteBtnActionPerformed

    private void notionRefreshBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_notionRefreshBtnActionPerformed
        for (int i = notionTableModel.getRowCount() - 1; i >= 0; i--) {
            notionTableModel.removeRow(i);
        }
        
        String text = descriptionTextArea.getText();
        String[] split = text.split("\\s+");

        if (split == null || split.length == 0) {
            return;
        }

        for (String string : split) {
            if (string != null && string.length() > 2) {
                notionTableModel.addRow(new Object[]{string});
            }
        }

        revalidate();
    }//GEN-LAST:event_notionRefreshBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel descriptionPanel;
    private javax.swing.JScrollPane descriptionScroll;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JPanel idPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton notionDeleteBtn;
    private javax.swing.JPanel notionPanel;
    private javax.swing.JButton notionRefreshBtn;
    private javax.swing.JScrollPane notionScrollPanel;
    private javax.swing.JTable notionTable;
    private javax.swing.JPanel prioritiesPanel;
    private javax.swing.JPanel priorityValuePanel;
    private javax.swing.JSpinner priorityValueSpiner;
    // End of variables declaration//GEN-END:variables

}
